package com.flong.spring.config;

import com.flong.spring.bean.MyDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

/**
 * @Author:liangjl
 * @Date: 2020/6/12-19:38
 * @Eamil:jilongliang@sina.com
 * @Description: Profile
 * Spring为我们提供的可以根据当前环境，动态激活和切换一系列组件的功能
 */

@PropertySource("classpath:/dataSource.properties")
public class ConfigOfProfile implements EmbeddedValueResolverAware {

    private StringValueResolver resolver;

    @Value("${jdbc.user}")
    private String user;

    private String driverClass;

    @Profile("test")
    @Bean("dataSourceTest")
    public MyDataSource dataSourceTest(@Value("${jdbc.pwd}")String pwd){
        return MyDataSource.builder().pwd(pwd).user(user).driverClass(driverClass).build();
    }

    @Profile("dev")
    @Bean("dataSourceDev")
    public MyDataSource dataSourceDev(@Value("${jdbc.pwd}")String pwd){
        return MyDataSource.builder().pwd(pwd).user(user).driverClass(driverClass).build();
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        String driverClass= resolver.resolveStringValue("${jdbc.driverClass}");
        System.out.println(driverClass);
        this.resolver = resolver;
        this.driverClass = driverClass;
    }
}
